package com.freak.freakmusic.base

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.freak.freakmusic.app.App
import com.freak.freakmusic.app.App.Companion.DESIGN_WIDTH
import com.freak.kotlinhttpmanager.kotlinhttpmanager.BasePresenter
import com.freak.kotlinhttpmanager.kotlinhttpmanager.log.LogUtil


/**
 * @author freak
 * @date 2019/9/11.
 * MVP activity基类
 */

abstract class BaseAbstractMvpActivity<V : BaseView,P:BasePresenter<V>> : BaseActivity(), BaseView {
    private var mPresenter: P? = null
    var mActivity: AppCompatActivity? = null

    /**
     * 绑定布局
     *
     * @return
     */
    protected abstract val layout: Int

    /**
     * 初始化事件和数据
     * 网络请求不要放在此方法中，因为断网重连时不会调用此方法，只会调用onCreateLoadData和onResumeLoadData
     */
    protected abstract fun initEventAndData()

    /**
     * 在onCreate方法中加载数据
     * 不需要刷新数据则请求数据在此方法中调用
     */
    protected abstract fun onCreateLoadData()

    /**
     * 资源释放
     */
    protected abstract fun onDestroyRelease()

    /**
     * 在onResume中加载数据
     * 需要返回刷新数据则选择放在这个方法中加载，否则在onCreateLoadData中加载数据
     */
    protected abstract fun onResumeLoadData()

    /**
     * 控件初始化
     */
    protected abstract fun initView()

    /**
     * 创建Presenter
     *
     * @return
     */
    protected abstract fun createPresenter(): P

    @SuppressLint("RestrictedApi")
    override fun onCreate(savedInstanceState: Bundle?) {
        /**
         * 创建presenter对象
         */
        if (mPresenter == null) {
            mPresenter = createPresenter()
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        }

        App.resetDensity(this, DESIGN_WIDTH.toFloat())
        setContentView(layout)
        super.onCreate(savedInstanceState)
        //绑定初始化ButterKnife
//        ButterKnife.bind(this)
        mActivity = this
        mPresenter?.attachView(this as V)?:LogUtil.e("Presenter 为空")
        initView()
        initEventAndData()
        onCreateLoadData()
    }


    override fun onDestroy() {
        super.onDestroy()
        /**
         * presenter 解除view订阅
         */
        if (mPresenter != null) {
            mPresenter!!.detachView()
        }
        onDestroyRelease()
    }

    override fun onResume() {
        super.onResume()
        onResumeLoadData()
    }

    override fun onPause() {
        super.onPause()
    }

    /**
     * 网络重连重新加载
     */
    override fun onNetConnectedReLoad() {
        super.onNetConnectedReLoad()
        onCreateLoadData()
        onResumeLoadData()
    }

    /**
     * 打开一个Activity 默认 不关闭当前activity
     *
     * @param className
     */
    fun gotoActivity(className: Class<*>) {
        gotoActivity(className, false)
    }

    /**
     * 打开一个Activity  关闭当前activity
     *
     * @param className
     */
    fun gotoActivityWithFinish(className: Class<*>) {
        gotoActivity(className, true)
    }

    /**
     * 打开一个Activity，并控制是否finish
     *
     * @param className              className
     * @param isCloseCurrentActivity 是否关闭
     */
    fun gotoActivity(className: Class<*>, isCloseCurrentActivity: Boolean) {
        gotoActivity(className, isCloseCurrentActivity, null)
    }

    /**
     * 打开一个activity，并传递数据
     *
     * @param className              className
     * @param isCloseCurrentActivity 是否关闭
     * @param bundle                 bundle数据
     */
    fun gotoActivity(
        className: Class<*>,
        isCloseCurrentActivity: Boolean = false,
        bundle: Bundle? = null
    ) {
        val intent = Intent(this, className)
        if (bundle != null) {
            intent.putExtras(bundle)
        }
        startActivity(intent)
        if (isCloseCurrentActivity) {
            finish()
        }
    }

    /**
     * 打开一个带结果返回的activity，并传递数据
     *
     * @param className   className
     * @param bundle      bundle数据
     * @param requestCode 请求码
     */
    fun gotoActivityWithResult(className: Class<*>, bundle: Bundle?, requestCode: Int) {
        val intent = Intent(this, className)
        if (bundle != null) {
            intent.putExtras(bundle)
        }
        startActivityForResult(intent, requestCode)
    }

    /**
     * 系统返回键监听
     *
     * @param item
     * @return
     */
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                super.onBackPressed()//返回
                return true
            }
            else -> return super.onOptionsItemSelected(item)
        }
    }

}
